<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
<title>mcq</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>
<link href='http://fonts.googleapis.com/css?family=Dosis' rel='stylesheet' type='text/css'>
</head>
<body>
<?php 
require 'functions.php';
$pickSomething = "";
////////////////////////////////////////////////////////////////////////////////////////
//																		   RUN FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
$getStudent = getStudent();
$pageQuestion = 1;
if (isset($_POST['submitter'])) // NOT SUBMITTED ANSWER
{
	$pageQuestion = $_POST['num']+1;
}
	 
if (isset($_POST['solicitFeedback'])) { //IF THEY'VE REQUESTED FEEDBACK USE CURRENT NUM
	$pageQuestion = $_POST['num'];
}

$getQuestion = getQuestion($pageQuestion);
////////////////////////////////////////////////////////////////////////////////////////
//																HANDLE ANSWER SUBMISSION
////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['submitter']))
{
$selectedAnswer = $_POST['questionOptions'];
   $checkAnswer = checkAnswer();
    $correctAnswer = $checkAnswer[0][0];
    if ($selectedAnswer == $correctAnswer) {
			$score = ($_POST['score']+1);
			if (!isset($_SESSION['rightAnswers'])) {
				$_SESSION['rightAnswers'] = array();
			}
			$_SESSION['rightAnswers'][] = $selectedAnswer;
			//$correct = $_SESSION['correct'];	
			$feedbackStatus = "<span class='green'> correct.</span>";
        } 
		else {
			
			if (!isset($_SESSION['wrongAnswers'])) {
							$_SESSION['wrongAnswers'] = array();
			}

		
			$_SESSION['wrongAnswers'][] = $selectedAnswer;
$feedbackStatus = "<span class='red'> incorrect</span>, the correct answer was $correctAnswer.";
			$score = ($_POST['score']); 
		}		
 $con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
		$query = "INSERT INTO studentresponse (attempt, studentID, selectedAnswer, questionID, testID, correctAnswer, startTime) VALUES ("
		. "'" . $_SESSION['attempt'] . "',"
		. "'" . $_SESSION['studentID'] . "',"
		. "'" . $selectedAnswer . "',"
		. "'" . $_POST['num']  . "',"
		. "'" . $testID  . "',"
		. "'" . $checkAnswer[0][0] . "', NOW()"
		. ")";	
$result = mysqli_query($con, $query);  
//$score = $_SESSION['score']; 
}
if (isset($_POST['solicitFeedback']))
{
    $num = $_POST['num'];
 	$_SESSION['score'] = $_POST['score']; 
  $score = $_SESSION['score'] ;
} 
////////////////////////////////////////////////////////////////////////////////////////
//																	RECORD STUDENTINTEST
////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['submitter'])){
	if ($_POST['num'] < 2)
{
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
		$query = "INSERT INTO studentintest (attempt, studentID, testID, startTime) VALUES ("
	. "'" . $_SESSION['attempt']  . "',"
	. "'" . $_SESSION['studentID'] . "',"
	. "'" . $testID  . "',"
	. "NOW()" 
	. ")";	
$result = mysqli_query($con, $query);  
}
}
if (isset($_POST['results'])){
		$query2 = "UPDATE studentintest SET endTime="
	. "NOW()," 
	. " finalScore = " 
	. "'" . $_SESSION['score'] . "'"
	. " WHERE studentID = "
	. "'" . $_SESSION['studentID'] . "' AND testID =  "
	. "'" . $testID  . "'";

$result = mysqli_query($con, $query2); 
if (isset($_POST['results']) && $_SESSION['attempt'] == 2 )
{
header('LOCATION: result2.php');
}
else{
header('LOCATION: result.php');
}
}
?>
<header> Formative Assessment </header>
<nav>
<?php include 'nav.php' ?>
</nav>
<div id="container" class="one-edge-shadow">
  <progress id='progress' value='<?php echo ($_POST['num']) ?>' max='11'>
    <?php echo ($_POST['num'])?>
  </progress>
  <div id="mainContent">
    <div id="quiz">
      <?php if (!isset($_POST['submitter'])) { 
   if ($getQuestion) {
		$questionID = $getQuestion[0][0];
		$questionStem = $getQuestion[0][1];
		$option1 = $getQuestion[0][2];
		$option2 = $getQuestion[0][3];
		$option3 = $getQuestion[0][4];
		$option4 = $getQuestion[0][5];
		$resource = $getQuestion[0][7];
} 
	  else {
	  echo "<p class='crying'>Could not get item</p>";			
	  }  ?>
      <form id="question" method="post">
        <h2>Question
          <?php echo $num; ?>
        </h2>
        <?php if ($resource){ echo "<div id='resource'>" ?>
        <?php echo $resource ?>
        <?php echo '</div>'  ?>
        <?php } ?>
        
        
   
        
      
        <div class="q-stem">
          <p id="itemStem">
            <?php echo $questionStem ?>
          </p>
        </div>
        <div>
          <input name="questionOptions" id="radio1" type="radio" value="<?php echo $option1 ?>">
          <label id="label1" for="radio1">
            <?php echo $option1 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio2" type="radio" value="<?php echo $option2 ?>">
          <label id="option2" for="radio2">
            <?php echo $option2 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio3" type="radio" value="<?php echo $option3 ?>">
          <label id="option3" for="radio3">
            <?php echo $option3 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio4" type="radio" value="<?php echo $option4 ?>">
          <label id="option4" for="radio4">
            <?php echo $option4 ?>
          </label>
        </div>
         <span class="error"> <?php echo $pickSomething;?></span>
        <input type="hidden" name="num" value="<?php echo $num ?>" />

        <input type="hidden" name="score" value="<?php echo $score ?>" />
        <input type="hidden" name="submitter" value="TRUE" />
       <input type="submit" id="submit" name="submit" value="Check Answer" class="btn" />
      </form>
      <?php } elseif (isset($_POST['submitter'])) { $num = $_POST['num'];  $_SESSION['score'] = $score; if($_POST['num']>9){$last = true; $_SESSION['finished'] = 'yes';} ?>
      <form id="feedback" method="post">
        <h2>Question
          <?php echo ($_POST['num']) ?>
        </h2>
        <p>You answered:
          <?php echo $selectedAnswer ?>
        </p>
        <p>That was
          <?php echo $feedbackStatus?>
        </p>
        <?php if ((!$last) && (isset($_POST['questionOptions']))) {  ?>
        <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
        <input type="hidden" name="score" value="<?php echo $score ; ?>" />
        <input type="hidden" name="solicitFeedback" value="TRUE" />
        <input type="submit" id="submit" name="next" value="Next Question" class="btn" />
        <?php } else { ?>
        <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
        <input type="hidden" name="score" value="<?php echo $_SESSION['score'] ; ?>" />
        <input type="submit" id="results" name="results" value="Get Results" class="btn" />
        <?php } ?>
      </form>
      <?php }?>
      <p class='smiling'>Incorrect Answers:
        <?php echo print_r(array_values($_SESSION['wrongAnswers']))?>
      </p>
      <p class='smiling'>Correct Answers:
        <?php echo print_r(array_values($_SESSION['rightAnswers']))?>
      </p>
      <p class='smiling'>Selected Answers:
        <?php echo $selectedAnswer ?>
      </p>
      <p class='smiling'>Score:
        <?php echo $score ?>
      </p>
      <p class='smiling'>Num:
        <?php echo $num ?>
      </p>
      <p class='smiling'>Last:
        <?php echo $last ?>
        |
        <?php echo ($_SESSION['finished'])?>
      </p>
      <p class='smiling'>Session score:
        <?php echo ($_SESSION['score'])?>
      </p>
      <p class='smiling'>Student ID:
        <?php echo ($_SESSION['studentID'])?>
      </p>
    </div>
  </div>
</div>
</body>
</html>
